﻿////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	ElencoFiles
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SQLite;
using System.Globalization;

namespace Map_Fvg
{
    public partial class ElencoFiles : Form
    {
        public static string InitialDirectory;
        public static string sezioneFound;
        private int idir;

        public ElencoFiles()
        {
            InitializeComponent();

            idir = 0;
            int icomp = InitialDirectory.CompareTo(FormIniz.cartellaSezioni);
            if (icomp == 0) idir = 1;
            else
            {
                icomp = InitialDirectory.CompareTo(FormIniz.cartellaElementi);
                if (icomp == 0) idir = 2;
            }
            if (idir == 0)
            {
                icomp = InitialDirectory.CompareTo(FormIniz.cartellaGPS);
                if (icomp == 0) idir = 3;
            }

            if (idir == 0)
            {
                icomp = InitialDirectory.CompareTo(FormIniz.cartellaElementi_isolati);
                if (icomp == 0) idir = 4;
            }

            if (idir == 0)
            {
                MessageBox.Show("ElencoFiles: errore");
                return;
            }

            lista_elementi(idir);
            textBox1.Text = InitialDirectory;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string curItem = listBox1.SelectedItem.ToString();

            int iret = verificaFiles(curItem);
            if (iret == 0)
            {
                if (idir == 1)
                {
                    string ss = curItem.Substring(0, 6);
                    sezioneFound = FormIniz.cartellaSezioni;
                    sezioneFound = string.Concat(sezioneFound, ss);
                }
                if (idir == 2)
                {
                    int ipos = curItem.IndexOf(' ', 0);
                    string ss = curItem.Substring(0, ipos);
                    sezioneFound = FormIniz.cartellaElementi;
                    sezioneFound = string.Concat(sezioneFound, ss, ".zip");
                }
                if (idir == 3)
                {
                    sezioneFound = FormIniz.cartellaGPS;
                    sezioneFound = string.Concat(sezioneFound, curItem);
                }
                if (idir == 4)
                {
                    sezioneFound = FormIniz.cartellaElementi_isolati;
                    sezioneFound = string.Concat(sezioneFound, curItem);
                } 
            }
        }

        private SQLiteConnection SetConnection()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.quadroUnione, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }

        int verificaFiles(string elementName)
        {


            return 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string caption = string.Empty;

            string arg = string.Empty;
            if(idir == 1) arg = "si vuole cancellare la sezione " + sezioneFound + " ? ";
            if(idir == 2) arg = "si vuole cancellare l'elemento " + sezioneFound + " ? ";
            if(idir == 3) arg = "si vuole cancellare if file " + sezioneFound + " ? ";
            if (idir == 4) arg = "si vuole cancellare l'elemento " + sezioneFound + " ? ";

            var result = MessageBox.Show(arg, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result != DialogResult.Yes) return;
             
            if (idir == 1 )
            {
               Directory.Delete(sezioneFound, true);
            }

            else //if (idir == 2 || idir == 3 || idir == 4)
            {
                try
                {
                    File.Delete(sezioneFound);
                }

                catch (Exception Ex)
                {
                    MessageBox.Show("chiudere l'applicazione " + Ex.ToString());
                    return;
                }
            }

            listBox1.Items.Clear();

            lista_elementi(idir);

        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
 
        }

        private void lista_elementi(int ldir)
        {
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            if (ldir == 1)
            {
                label1.Text = "Cartella  sezioni";
                string[] fileEntries = Directory.GetDirectories(InitialDirectory);

                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    foreach (string fileName in fileEntries)
                    {
                        string ss = string.Empty;
                        int ipos = fileName.LastIndexOf('\\');
                        int len = fileName.Length;
                        ++ipos;
                        if (ipos > 0) ss = fileName.Substring(ipos, len - ipos);
                        using (SQLiteCommand cmd = new SQLiteCommand(conn))
                        {
                            cmd.CommandText = "select nome from QU_10000 where codice = '" + ss + "'";
                            SQLiteDataReader reader = cmd.ExecuteReader();
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    string row = reader.GetString(0).ToString();
                                    ss = string.Concat(ss, "      ", row);
                                }
                            }


                            listBox1.Items.Add(ss);
                        }
                    }
                    conn.Close();
                }
            }

            if (ldir == 2)
            {
                label1.Text = "Cartella  elementi";

                string[] fileEntries1 = Directory.GetFileSystemEntries(InitialDirectory, "*.zip");
                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    foreach (string fileName in fileEntries1)
                    {
                        string ss = string.Empty;
                        int ipos = fileName.LastIndexOf('\\');
                        int len = fileName.Length;
                        ++ipos;
                        if (ipos > 0) ss = fileName.Substring(ipos, len - ipos);
                        string sss = ss.Substring(0, 6);

                        using (SQLiteCommand cmd = new SQLiteCommand(conn))
                        {
                            cmd.CommandText = "select nome from QU_5000 where codice = '" + sss + "'";
                            SQLiteDataReader reader = cmd.ExecuteReader();
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    string row = reader.GetString(0).ToString();
                                    ipos = ss.IndexOf(".");
                                    if(ipos > 0) ss = ss.Substring(0,ipos);
                                    ss = string.Concat(ss, "      ", row);
                                }
                            }
                            listBox1.Items.Add(ss);
                        }
                    }
                    conn.Close();
                }
            }

            if (ldir == 3)
            {
                label1.Text = "Cartella  gps";
                string[] fileEntries = Directory.GetFileSystemEntries(InitialDirectory, "*.gpx");
                foreach (string fileName in fileEntries)
                {
                    string ss = string.Empty;
                    int ipos = fileName.LastIndexOf('\\');
                    int len = fileName.Length;
                    ++ipos;
                    if (ipos > 0) ss = fileName.Substring(ipos, len - ipos);

                    listBox1.Items.Add(ss);

                }
            }
            textBox1.Text = InitialDirectory;

            if (ldir == 4)
            {
                label1.Text = "Cartella  elementi isolati";
                string[] fileEntries = Directory.GetFileSystemEntries(InitialDirectory, "*.zip");
                foreach (string fileName in fileEntries)
                {
                    string ss = string.Empty;
                    int ipos = fileName.LastIndexOf('\\');
                    int len = fileName.Length;
                    ++ipos;
                    if (ipos > 0) ss = fileName.Substring(ipos, len - ipos);

                    ipos = ss.LastIndexOf('\\');
                    len = ss.Length;
                    ++ipos;
                    if (ipos > 0) ss = ss.Substring(ipos, len - ipos);
                    listBox1.Items.Add(ss);

                }
            }
            textBox1.Text = InitialDirectory;
        }

        private static string GetCodePoint(char ch)
        {
            string retVal = "u+";
            byte[] bytes = Encoding.Unicode.GetBytes(ch.ToString());
            for (int ctr = bytes.Length - 1; ctr >= 0; ctr--)
                retVal += bytes[ctr].ToString("X2");

            return retVal;
        }

    }
}
